<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\MemberRepositoryEloquent;

use App\User;
use App\Models\Commission;
use Breadcrumbs, Toastr,Config,Validator,DB;
use Illuminate\Http\Request;
use App\Models\FinancialDetails;

class CommissionController extends BaseController{

    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    public function index(Request $request){
        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }

        $commissions = Commission::where($where)
                        ->whereHas('commission_user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->orderBy('id','desc')->paginate(20);
        return view('admin.finance.commission',compact('commissions','data'));
    }

    public function settle(Request $request){
        $datas = $request->all();
        //$commissions = Commission::whereIn('id',$datas['ids'])->groupBy('commission_user_id')->get();
        $commissions = Commission::where('status','=',0)->whereIn('id',$datas['ids'])->get();
        $collection = collect($commissions);
        $groups = $collection->groupBy('commission_user_id');
        DB::beginTransaction();
        try{ 
            $result = null;
            foreach($groups as $commission_user_id=>$g){
                $res=Commission::where('status','=',0)->where('commission_user_id','=',$commission_user_id)->whereIn('id',$datas['ids'])->update(['status'=>1]);
                if($res){
                    $summoney = collect($g)->sum('commission');
                    $result = $this->user->modifyBalance($commission_user_id,$summoney,6,'佣金结算');
                }
            }
            DB::commit(); 
            return response()->json($result ? ['status' => 1] : ['status' => '-1','msg'=>'没有可结算的佣金']);
        }catch(\Exception $e){
            //接收异常处理并回滚 
            DB::rollBack(); 
            return response()->json(['status' => 0]);
        }

    }

    /**
     * [settleAll 结算所有]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function settleAll(Request $request){
        
        $commissions = Commission::where('status','=',0)->get();
        $collection = collect($commissions);
        $groups = $collection->groupBy('commission_user_id');
        DB::beginTransaction();
        try{
            if(!empty($groups)){ 
                $result = null;
                foreach($groups as $commission_user_id=>$g){
                    $res=Commission::where('status','=',0)->where('commission_user_id','=',$commission_user_id)->update(['status'=>1]);
                    //var_dump($res);
                    if($res){
                        $summoney = collect($g)->sum('commission');
                        //var_dump($summoney);
                        //var_dump($commission_user_id);
                        $result = $this->user->modifyBalance($commission_user_id,$summoney,6,'佣金结算');
                         //var_dump($result);
                    }
                }
            }
            DB::commit(); 
            return response()->json($result ? ['status' => 1] : ['status' => -1,'msg'=>'没有可结算的佣金']);
        }catch(\Exception $e){
            //接收异常处理并回滚 
            DB::rollBack(); 
            return response()->json(['status' => 0]);
        }

    }

    public function cancel(Request $request){
        dd($request->all());
    }

}